package com.vma.manageSystem.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.vma.advertisement.model.Advertisement;
import com.vma.manageSystem.util.ReturnsUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/3/29.
 */
public class AdvertiseController extends Controller {
//    title:title,
//    url:url,
//    type:type,
//    endTime:endTime,
//    ifLink:ifLink,
//    content:editor.$txt.html()
    public void add(){
        try {
            String title = getPara("title");
            String url = getPara("url");
            String type = getPara("type");
            String endTime = getPara("endTime");
            String ifLink = getPara("ifLink");
            String content = getPara("content");
            String imgUrl = getPara("imgUrl");
            Advertisement advertisement = new Advertisement();
            if(url != null && !"".equals(url)){
                advertisement.setUrl(url);
            }
            if(endTime != null && !"".equals(endTime)){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                advertisement.setEndTime(format.parse(endTime+" 23:59:59"));
            }
            advertisement.setTitle(title).setType(type).setIfLink(ifLink)
                    .setImgUrl("http://121.196.211.102:41200"+imgUrl).setAStatus("1").
                    setCreateTime(new Date()).setContent(content).setIfDelete("0").save();
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
        }



    }

    public void update(){
        try{
            String id = getPara("id");
            String status = getPara("status");
            Advertisement.dao.findById(id).setAStatus(status).update();
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void qryAdList(){
        String status = getPara("status");
        String title = getPara("title");
        String start = getPara("start");
        String end = getPara("end");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(status != null && !"".equals(status)){
            condition += "and a_status = ? ";
            param.add(status);
        }

        if(title != null && !"".equals(title)){
            condition += "and title like ? ";
            param.add("%"+title+"%");
        }
        if(start != null && !"".equals(start)){
            condition += "and create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and create_time < ? ";
            param.add(end);
        }

        Page<Advertisement> advertisementPage = Advertisement.dao.paginate(pageNo,pageSize,"select * ","from advertisement where 1=1 "+condition+
                        " order by create_time desc"
                ,param.toArray());
        renderJson(ReturnsUtil.returnSuccess(advertisementPage));
    }
}
